package com.bbs.controller;
import com.bbs.entity.*;
import com.bbs.service.*;
import com.bbs.utils.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/")
public class PostController {
    @Autowired
    private TopicService topicService;
    @Autowired
    private PostService postService;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private PostUserService postUserService;
    @Autowired
    private UserService userService;


    //去发帖前的页面
    @RequestMapping("/toPublish")
    public String toPublish(Model model, HttpSession session){
        if (session.getAttribute("uid") == null){
            return "login";
        }

        List<Topic> topicsList = topicService.listTopic();
        model.addAttribute("topicList",topicsList);//给模板添加数据
        return "publish";
    }

    //去帖子详细页面
    @RequestMapping("/toPost")
    //@ResponseBody
    public String toPost(int pid,Model model,HttpSession session){
        Integer sessionUid = (Integer)session.getAttribute("uid");
        //获取帖子信息
        Post post = postService.getPostByPid(pid);
        System.out.println(post.getId());
        //获取评论信息
        List<Reply> replyList =  replyService.listReply(post.getId());
        //判断用户是否点赞
        boolean liked = false;
        if (sessionUid !=null){
            PostUser postUser = postUserService.findByPIdAndUid(pid,sessionUid);
            if (postUser !=null){
                liked = true;
            }
        }
        //向模型添加数据
        model.addAttribute("post",post);
        model.addAttribute("replyList",replyList);
        model.addAttribute("liked",liked);
        return "post";
    }

    /*
    *发帖
    * @param post
    * @return
    */
    @RequestMapping("/publishPost")
    //一个请求，只有一个RequestBody；一个请求，可以有多个RequestParam。
    @ResponseBody
    //@RequestBody 
    // @RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)
    public ResultInfo publishPost(@RequestBody Post post){
        System.out.println(post);
        int flag = postService.publishPost(post);
        System.out.println("发帖成功"+flag);
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setMsg("发布成功");
        resultInfo.setCode("0");
        return resultInfo;
    }



    //    评论
    @RequestMapping("/reply")
    @ResponseBody
    public ResultInfo reply(@RequestBody Reply reply,HttpSession session){
        System.out.println(reply);
        int uid = (int)session.getAttribute("uid");
        reply.setReplyTime(MyUtil.formatDate(new Date())); //显示当前时间
        reply.setUid(uid);
        int flag = replyService.insert(reply);
        ResultInfo resultInfo = new ResultInfo();
        if (flag==1){
            resultInfo.setMsg("评论成功");
            resultInfo.setCode("0");
        }else {
            resultInfo.setMsg("评论失败");
            resultInfo.setCode("1");
        }
        return resultInfo;
    }


/**
 * 点赞
 */
    @RequestMapping("/like")
    @ResponseBody
public ResultInfo like(int pid,HttpSession session){
        int uid = (int)session.getAttribute("uid");
        System.out.println(pid+"-------"+uid);
        postUserService.save(new PostUser(pid,uid));
        //个人网页获取点赞数
        userService.updatelikeCount(uid);
        Post post = postService.getPostByPid(pid);
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(""+post.getLikeCount());
        resultInfo.setMsg("已点赞");
        return resultInfo;
    }

    /**
     * 楼中楼回复
     * @param pid
     * @param rid
     * @param content
     * @param session
     * @return
     */

    @RequestMapping("/comment")
    public String comment(int pid,int rid,String content,HttpSession session){
        Integer uid = (Integer)session.getAttribute("uid");
        if (uid == null){
            return "redirect:toLogin";
        }
        int sessionUid = (int) session.getAttribute("uid");
        replyService.comment(pid,sessionUid,rid,content);
        return "redirect:toPost.do?pid="+pid;
    }
}
